home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-03-30 | 4.8 KB | 137 lines | [TEXT/GEOL] |
- Item 7587233 29-March-90 02:29PST
-
- From: POWERUP.ENG Power Up Software,PRT
-
- To: MACAPP.TECH$ MacApp Technical
-
- Sub: FindObject() and FindValu
-
- Attn: MacApp.Tech$
- SentBy: James Plamondon
- Date 3/28/90
- Subject FindObject() and FindValue(
- From James Plamondon
- To MacApp.Tech$
- CC Steve Friedrich
-
- Subject: FindObject() and FindValue()
- Gentlepersons,
-
- I've been using TSortedLists a lot lately, and I've hit upon a small extension
- to the class that adds a lot of value to it — or so it seems to me. I'd be
- interested in hearing your opinions.
-
- Consider the function TSortedList.Search(). It accepts a function,
- TestItem(), as an argument, and returns the first item encountered (in a
- binary search) for which TestItem() returns 0. TestItem() is expected to be a
- function which accepts an object as its only argument, and which returns 0
- (kItem1EqualsItem2) if the given argument is "the right one."
-
- So far, all well and good — except that I keep having to include the #$!@¡
- TestItem() function in my code, as a local function to the routine that calls
- Search(). This wastes my time, clutters my code, and increases the chance of
- error — specifically, that the various versions of TestItem() won't match.
-
- I propose what I think is a simple (if not necessarily elegant) solution — the
- addition of three new functions to TSortedList. Here are their interfaces:
-
- FUNCTION TSortedList.FindObject(theObject: TObject): TObject;
- { This function returns an object in the list which is the same as the
- given object,
- according to Compare(). }
-
- FUNCTION TSortedList.FindValue(valuePtr: Univ Ptr): TObject;
- { This function returns an object in the list which has the same value as
- that pointed
- to by the given pointer, according to CompareValue(). }
-
- FUNCTION TSortedList.CompareValue(item: TObject; valuePtr: Univ Ptr):
- CompareResult;
- { This function compares the given object to the value pointed to by the
- given pointer.
- THIS FUNCTION MUST BE OVERRIDEN for it to make any sense. }
-
- • Here are the implementations of FindObject() and FindValue() for
- TSortedList:
-
- FUNCTION TSortedList.FindObject(theObject: TObject): TObject;
- FUNCTION TestItem(item: TObject): CompareResult;
- BEGIN
- TestItem := Compare(item, theObject);
- END; { TestItem }
-
- BEGIN { FindObject }
- FindObject := Search(TestItem);
- END; { FindObject }
-
- FUNCTION TSortedList.FindValue(valuePtr: Univ Ptr): TObject; OVERRIDE;
- FUNCTION TestItem(item: TObject): CompareResult;
- BEGIN
- TestItem := CompareValue(item, valuePtr);
- END; { TestItem }
-
- BEGIN { FindValue }
- FindValue := Search(TestItem);
- END; { FindValue }
-
-
- • Here is an example of how these routines would be used, for a sorted list
- TFooList containing references to objects of class TFoo:
-
- TFoo = OBJECT(TObject)
- fValue: LONGINT; { for example }
- END; { TFoo }
-
- TFooList = OBJECT(TSortedList)
- FUNCTION TFooList.Compare(item1, item2: TObject): CompareResult;
- OVERRIDE;
- FUNCTION TFooList.CompareValue(item: TObject; valuePtr: Univ Ptr):
- CompareResult;
- OVERRIDE;
- END; { TFooList }
-
- FUNCTION TFooList.Compare(item1, item2: TObject): CompareResult;
- OVERRIDE;
- VAR
- value: LONGINT;
-
- BEGIN
- value := TFoo(item2).fValue;
- Compare := CompareValue(item1, @value);
- END; { Compare }
-
- FUNCTION TFooList.CompareValue(item: TObject; valuePtr: Univ Ptr):
- CompareResult;
- OVERRIDE;
- BEGIN
- CompareValue := TFoo(item1).fValue - LongIntPtr(valuePtr)^;
- END; { CompareValue }
-
-
- • Hark! I can already hear the cries of outrage at the thought of passing an
- untyped pointer (valuePtr) around, and casting it to the "expected" type. But
- that does not appear to me to be any worse than casting the type of the given
- objects from TObjects to TFoo's, which one must already do in a TList's
- Compare() method.
-
- The advantage, of course, is that I can just override Compare() and
- CompareValue(), and I'm done. I can use FindObject() and FindValue() instead
- of cluttering my code with a lot of friggin' TestItem()/Search() calls.
-
- The only caveat that I can see is that the routine calling CompareValue()
- should make sure that it's not passing the address of an instance variable as
- valuePtr, since the comparison routine might be in a different segment (and
- thus move memory, making the address invalid). Since valuePtr is a universal
- pointer, it can contain a reference to just about anything.
-
- So — whadduya think? Would this be a useful extension to TSortedList, or is
- this yet another sign of a warped mind and a twisted way of looking at the
- world?
-
- Looking forward to your responses, I remain
-
- Yours,
-
- James Plamondon
-
-